import { Effect, Reducer, Subscription } from 'umi';
import { gettags, readingList, getlabel, getDetaillist } from "@/services";
import { listItem, tagslistItem, labelItem } from "@/utils/Article"
export interface IndexModelState {
  tagslist: Array<tagslistItem>
  readList: Array<listItem>,
  labellist: Array<labelItem>,
  articlelist: Array<listItem>,
}

export interface IndexModelType {
  namespace: 'Atag';
  state: IndexModelState;
  effects: {
    gettags: Effect,
    readinglist: Effect;
    getlabel: Effect,
    getDetaillist: Effect
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions?: { setup: Subscription };
}

const IndexModel: IndexModelType = {
  namespace: 'Atag',

  state: {
    tagslist: [],
    readList: [],
    labellist: [],
    articlelist: []
  },

  effects: {
    //获取标签
    *gettags({ payload }, { call, put }) {
      let res = yield call(gettags)
      yield put({
        type: "save",
        payload: {
          tagslist: res.data
        }
      })

    },
    //获取轮播图数据

    *readinglist({ payload }, { call, put }) {
      let result = yield call(readingList)
      yield put({
        type: 'save',
        payload: {
          readList: result.data
        }
      })
    },
    *getlabel({ payload }, { call, put }) {
      let result = yield call(getlabel)
      yield put({
        type: 'save',
        payload: {
          labellist: result.data
        }
      })
    },
    *getDetaillist({ payload }, { call, put }) {
      let res = yield call(getDetaillist, payload.value)
      yield put({
        type: 'save',
        payload: {
          articlelist: res.data[0]
        }
      })
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    // 启用 immer 之后
    // save(state, action) {
    //   state.name = action.payload;
    // },
  },

};

export default IndexModel;